package com.sunny.hp.common.util;

public class StringUtil {

	/** 문자열 복사
	 * @param x
	 * @param y
	 * @return
	 */
	public String getStr2Str(String x, String y){
		if(y != null){
			x = y;
		}
		return x;
	}

	/** 문자열을 숫자로 변환
	 * @param x
	 * @param y
	 * @return
	 */
	public int getStr2Int(int x, String y){
		if(y != null){
			x = Integer.parseInt(y);
		}
		return x;
	}


	/** 문자열을 실수로 변환
	 * @param x
	 * @param y
	 * @return
	 */
	public Object getStr2Double(Object x, String y){

		if(y != null){
			if(x.getClass().getCanonicalName().equals("float")){
				x = Float.parseFloat(y);
			}else if(x.getClass().getCanonicalName().equals("double")){
				x = Double.parseDouble(y);
			}
		}
		return x;
	}

	/*-----------------------------------------------------------
	 * Length Definition
	 * --------------------------------------------------------*/

	public static int  LEN_JOB_DATE= 	8;
	public static int  LEN_JOB_TIME=	 	6;
	public static int  LEN_JOB_HOUR= 	2;
	public static int  LEN_CO_ID= 	10;
	public static int  LEN_CO_NM=  50;
	public static int  LEN_UNIT=  		15;
	public static int  LEN_ENERGY_VALUE=20;
	public static int  LEN_ENERGY_GROUP_ID=2;
	public static int  LEN_CONVERT_ID=  	2;
	public static int  LEN_CONVERT_NAME=50;
	public static int  LEN_ENERGY_SYSTEM_GROUP_ID= 2;
	public static int  LEN_ENERGY_SYSTEM_ID=  15;
	public static int  LEN_POINT_VALUE_TYPE=  2;
	public static int  LEN_DDC_ID=10;
	public static int  LEN_POINT_ID= 10;
	public static int  LEN_REAL_VIRTUAL=1;
	public static int  LEN_EPATTERN_ID= 10;
	public static int  LEN_EPATTERN_VALUE_ID= 10;
	public static int  LEN_EPATTERN_FUNCTION_ID= 10;
	public static int  LEN_EFUNCTION_DEFINE=  1024;
	public static int  LEN_DIMENSION=3;
	public static int  LEN_PRICEPLAN_ID=2;
	public static int  LEN_PRICEPLAN_COMPANY= 50;
	public static int  LEN_PRICEPLAN_UNIT= 10;
	public static int  LEN_STAT_DATE=8;
	public static int  LEN_STAT_TIME=4;
	public static int  LEN_MAP_KEY=  100;
	
	/** 영문자(대소문자) 인지 확인
	 * @param schar
	 * @return
	 */
	public static boolean isAlpha(char schar){
		if((0x61 <= schar && schar <= 0x7A ) || ( 0x41 <= schar && schar <= 0x5A )){
			return true;
		}
		return false;
	}
	
	/** 숫자인지 여부 확인
	 * @param schar
	 * @return
	 */
	public static boolean isDigit(char schar){
		if(0x30 <= schar && schar <= 0x39 ){
			return true;
		}
		return false;
	}
	/**
	 * NVL
	 * @param str, strDefault
	 * @return String
	 * @throws
	 */
	public static String NVL(String str, String strDefault)
	{
		String r = strDefault;
		try
		{
			if (str != null)
				r = str;
		}
		catch(Exception e)
		{
			//throw e;
		}
		return r;
	}
	
	public static void main(String [] args){
		checkValidFunction("(123defg@!");
	}

	public static boolean checkValidFunction(String cpFunction){
		int iLbkCnt = 0;
		int iRbkCnt = 0;
		int iFuncMark = 0;
		
		int functionLength = cpFunction.length();
		if(functionLength < 6){
			//System.out.format("[ERROR] [%s] Wrong Biz Function! (too short length)\n", cpFunction);
			return false;
		}
		
		for(int i = 0 ; i < functionLength ; i++){
			char ccFc = cpFunction.charAt(i);
			if(isAlpha(ccFc) || isDigit(ccFc)){ // 영문자 숫자 제외 특수문자를 찾는다.
				continue;
			}else if(ccFc == '!'){
					if (i != 0)
		            {
						//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            }
					
					if (!isAlpha(cpFunction.charAt(i+1)) && !isDigit(cpFunction.charAt(i+1))) 
		            {
						//System.out.format("[ERROR] [%c] Wrong Location!\n", cpFunction.charAt(i+1));
		                return false;
		            }
					continue;
				}
			else if(ccFc == '('){
					if (!isAlpha(cpFunction.charAt(i-1)) && !isDigit(cpFunction.charAt(i-1))) 
		            {
						//System.out.format("[ERROR] [%c] Wrong Location!\n", cpFunction.charAt(i-1));
		                return false;
		            }

		            if (i != (functionLength - 1)) 
		            {
		                if (cpFunction.charAt(i+1) != '$') 
		                {
		                	//System.out.format("[ERROR] [%c] Wrong Location!\n", cpFunction.charAt(i+1));
		                    return false;
		                }
		            }
		            iLbkCnt++;
				}
				
			else if (ccFc == ')') 
		        {
		            iRbkCnt++;

		            if (!isAlpha(cpFunction.charAt(i-1)) && !isDigit(cpFunction.charAt(i-1))) 
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", cpFunction.charAt(i-1));
		                return false;
		            }

		            if (iRbkCnt > iLbkCnt) 
		            {
		            	//System.out.format("[ERROR] [%c] appeared first!\n", ccFc);
		                return false;
		            } 

		            if (i != (functionLength - 1)) 
		            {
		            	//System.out.format("[ERROR] [%c] is not last!\n", ccFc);
		                return false;
		            }

		            continue;
		        }
			else if (ccFc == '$') 
		        {
		            iFuncMark++;

		            if (iLbkCnt == 0)
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            } 
		            
		            if (i == (functionLength - 1)) 
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            } 
		            
		            if (!isAlpha(cpFunction.charAt(i+1)) && !isDigit(cpFunction.charAt(i+1))) 
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            }

		            continue;
		        }
			else if (ccFc == ',') 
		        {
		            if (iLbkCnt == 0)
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            } 
		            
		            if (i == (functionLength - 1)) 
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            } 
		            
		            if (!isAlpha(cpFunction.charAt(i-1)) && !isDigit(cpFunction.charAt(i-1))) 
		            {
		            	//System.out.format("[ERROR] [%c] Wrong Location!\n", ccFc);
		                return false;
		            }
		            continue;
		        }
			else 
	        {
				//System.out.format("[ERROR] [%c] is Not Enabled!\n", ccFc);
	            return false;
	        }
			}
		if (iLbkCnt != 1 || iRbkCnt != 1) 
	    {
			//System.out.format("[ERROR] Wrong Use Bracket! [%d][%d]\n", iLbkCnt, iRbkCnt);
	        return false;
	    }

	    if (iFuncMark <= 0)         // $ 는 반드시 1개 이상..
	    {
	    	//System.out.format("[ERROR] The number of $ is Wrong! [%d]\n", iFuncMark);
	        return false;
	    }

		return true;
	}
	
	public static String valueReplace(){
		return "";
	}
}
